package org.hongbo.springbootebsapi.result;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hongbo.springbootebsapi.enums.ResultCode;
import org.hongbo.springbootebsapi.jwt.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @desc 平台通用返回结果
 * 
 * @author zhumaer
 * @since 10/9/2017 3:00 PM
 */
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
@Component
public class PlatformResult implements Result {

	@Autowired
	JwtTokenUtil jwtTokenUtil;

	private static PlatformResult platformResult;

	private static final long serialVersionUID = 874200365941306385L;

	private Integer code;

	private String message;

	private String refreshToken;

	private Object data;

	public static PlatformResult success() {
		PlatformResult result = new PlatformResult();
		result.setResultCode(ResultCode.SUCCESS);
		return result;
	}

	//将自动注入引入到静态方法中
	public static PlatformResult success(Object data) {
		PlatformResult result = new PlatformResult();
		result.setRefreshToken(platformResult.jwtTokenUtil.getRefreshToken());
		result.setResultCode(ResultCode.SUCCESS);
		result.setData(data);
		return result;
	}

	public static PlatformResult failure(ResultCode resultCode) {
		PlatformResult result = new PlatformResult();
		result.setResultCode(resultCode);
		return result;
	}

	public static PlatformResult failure(ResultCode resultCode, Object data) {
		PlatformResult result = new PlatformResult();
		result.setResultCode(resultCode);
		result.setData(data);
		return result;
	}

	public static PlatformResult failure(String message) {
		PlatformResult result = new PlatformResult();
		result.setCode(ResultCode.PARAM_IS_INVALID.code());
		result.setMessage(message);
		return result;
	}

	private void setResultCode(ResultCode code) {
		this.code = code.code();
		this.message = code.message();
	}

	@PostConstruct
	public void init(){
		platformResult = this;
		platformResult.jwtTokenUtil = this.jwtTokenUtil;
	}

}
